package com.example.jones.function;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.constant.AppConstant;
import com.example.constant.VariableKey;
import com.example.jones.listener.AlarmOnClickListener;
import com.example.jones.test.MyColletion;
import com.example.jones.test.R;
import com.example.jones.test.UserLogin;

public class FunctionView {
    private Context mcontext = null;
    private View mView = null;
    private LoginView loginView = null;
    private LinearLayout Layout_1, function_Layout = null;
    private Button shoucang, fenxiang, qingdan, about, sugg, clear,
            login_function;

    public FunctionView(Context c) {
        this.mcontext = c;
    }

    public View getView() {
    mView = View.inflate(mcontext, R.layout.function, null);
        
        function_Layout = (LinearLayout) mView.findViewById(R.id.function_Layout);
        //Layout_1 = (LinearLayout) mView.findViewById(R.id.Layout_1);
        
        shoucang = (Button) mView.findViewById(R.id.shoucang);
        fenxiang = (Button) mView.findViewById(R.id.fenxiang);
        qingdan = (Button) mView.findViewById(R.id.qingdan);
        about = (Button) mView.findViewById(R.id.about);
        sugg = (Button) mView.findViewById(R.id.sugg);
        clear = (Button) mView.findViewById(R.id.clear);
        login_function = (Button) mView.findViewById(R.id.login_function);

        System.out.println("####################进入FunctionView");

        Object obj = AppConstant.GLOBAL_VARIABLE.get(VariableKey.USER_ID);
        String userID = obj == null ? "" : (String) obj;
        if ((null != userID) && (!userID.isEmpty())) {
            login_function.setText("退出登录");
            login_function.setBackgroundColor(Color.RED);

            login_function.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    logout();
                }
            });
        } else {
            login_function.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    logoinView();
                }
            });
        }

        shoucang.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!AppConstant.GLOBAL_VARIABLE
                        .containsKey(VariableKey.USER_ID)) {
                    Toast.makeText(mcontext, "请先登录", Toast.LENGTH_SHORT).show();

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(2000L);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Intent intent = new Intent(mcontext,
                                    UserLogin.class);
                            mcontext.startActivity(intent);
                        }
                    }).start();

                } else {
                    Intent intent = new Intent(mcontext, MyColletion.class);
                    mcontext.startActivity(intent);
                }
            }
        });

        qingdan.setOnClickListener(new AlarmOnClickListener());

        return mView;
    }

    private void logout() {
        AppConstant.GLOBAL_VARIABLE.remove(VariableKey.USER_ID);

        login_function.setText("登 录");
        login_function.setBackgroundColor(Color.GRAY);

        login_function.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                logoinView();
            }
        });
    }

    private void logoinView() {
        Intent it = new Intent(mcontext, UserLogin.class);
        mcontext.startActivity(it);
    }

}